package com.eduhzy.ttw.clazz.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.clazz.R;
import com.eduhzy.ttw.clazz.R2;
import com.eduhzy.ttw.clazz.di.component.DaggerGroupListComponent;
import com.eduhzy.ttw.clazz.di.module.GroupListModule;
import com.eduhzy.ttw.clazz.mvp.contract.GroupListContract;
import com.eduhzy.ttw.clazz.mvp.model.entity.HomeData;
import com.eduhzy.ttw.clazz.mvp.model.entity.ReviewGroupData;
import com.eduhzy.ttw.clazz.mvp.presenter.GroupListPresenter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-12-6 15:19:30
 */
@Route(path = RouterHub.CLAZZ_GROUPLISTACTIVITY)
public class GroupListActivity extends BaseActivity<GroupListPresenter> implements GroupListContract.View {
    @Autowired(name = Constants.PARCELABLE_DATA)
    HomeData mBean;
    @Autowired(name = Constants.ROUTER_INTEGER)
    int teamType;
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;

    @Inject
    BaseQuickAdapter<ReviewGroupData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager mLayoutManager;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerGroupListComponent
                .builder()
                .appComponent(appComponent)
                .groupListModule(new GroupListModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        MobclickAgent.onEvent(getActivity(),"ttw_group_manage");
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.clazz_activity_group_list;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mAdapter.setHeaderFooterEmpty(true, true);
        mRvList.setLayoutManager(mLayoutManager);
        mRvList.setAdapter(mAdapter);
        mSwipeLayout.setOnRefreshListener(() -> getData());
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            ReviewGroupData data = mAdapter.getData().get(position);
            ARouter.getInstance().build(RouterHub.CLAZZ_EDITGROUPACTIVITY)
                    .withString(Intent.EXTRA_TITLE, "编辑小组")
                    .withParcelable(Constants.PARCELABLE_DATA, data)
                    .withInt(Constants.ROUTER_INTEGER, teamType)
                    .navigation(getActivity());
        });
        getData();
    }

    private void getData() {
        if (ObjectUtils.isNotEmpty(mBean)) {
            mPresenter.getTeamList(mBean.getClassId(), teamType);
        }
    }

    @Override
    public void showLoading() {
        mSwipeLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mSwipeLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Subscriber(tag = RouterHub.CLAZZ_GROUPLISTACTIVITY)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.EDIT_GROUP_SUCCESS:
                getData();
                break;
            default:
        }
    }

    @Override
    public void update(List<ReviewGroupData> data) {
        if (ObjectUtils.isEmpty(data)) {
            data = new ArrayList<>();
        }
        mAdapter.setNewData(data);
        if (mAdapter.getFooterLayoutCount() > 0) {
            mAdapter.removeAllFooterView();
        }

        View footer = View.inflate(getActivity(), R.layout.clazz_review_group_footer, null);
        ImageView ivIcon = footer.findViewById(R.id.iv_icon);
        ivIcon.setImageResource(R.drawable.img_tianjia);
        TextView tvTitle = footer.findViewById(R.id.tv_name);
        tvTitle.setText("添加组");
        mAdapter.addFooterView(footer);
        footer.setOnClickListener(view -> {
            ARouter.getInstance().build(RouterHub.CLAZZ_ADDGROUPACTIVITY)
                    .withString(Intent.EXTRA_TITLE, mBean.getClassName())
                    .withParcelable(Constants.PARCELABLE_DATA, mBean)
                    .withInt(Constants.ROUTER_INTEGER, teamType)
                    .navigation(getActivity());
        });
    }
}
